/**
 * 左侧边栏菜单
 */
HY.menu = function(){
	var
		$menu = $('#menu'),
		$mMitle = $menu.find('.m-title'),
		$mContent = $menu.find('.m-content'),
		$mCHead = $mContent.find('.m-c-head'),
		$mCDown = $mContent.find('.m-c-down'),
		index1 = 0,
		cur1 = null,
		curCon1 = null,
		index2 = 0,
		cur2 = null,
		curCon2 = null;
	$mMitle.on('click',function(){
		index1  = $mMitle.index($(this));
		curCon1 = $mContent.eq(index1);
		cur1 = $(this); 
		if(curCon1.is(':visible')){
			cur1.removeClass('cur');
			curCon1.hide();
		}else{
			curCon1.show();
			cur1.addClass('cur');
		}
	});
	$mCHead.on('click',function(){
		index2  = $mCHead.index($(this));
		curCon2 = $mCDown.eq(index2);
		cur2 = $(this); 
		if(curCon2.is(':visible')){
			cur2.removeClass('cur');
			curCon2.hide();
		}else{
			curCon2.show();
			cur2.addClass('cur');
		}
	});
	function init(){
		$mMitle.eq(index1).addClass('cur');
		$mContent.eq(index1).show();
		$mCHead.eq(index1).addClass('cur');
		$mCDown.eq(index1).show();
	}
	init();
}
/**
 * 条件筛选
 */
HY.attrs = function(){
	var
		$attrs = $('#attrs'),
		$Option = $attrs.find('.a-content'),
		$btnMore = $Option.find('.a-button'),
		$btnOk = $Option.find('.a-button2'),
		$btnCancel = $Option.find('.a-button3'),
		curIndex = 0,
		$links = null,
		$curOption = null,
		abcSelectIsOk = false,
		$attrsMore = $attrs.find('.attrs-m-btn'),
		attrsMoreText = '';
	//更多		
	$btnMore.on('click',function(){
		curIndex = $btnMore.index($(this));
		$curOption = $Option.eq(curIndex);
		$curOption.addClass('cur');
		if($curOption.data('abcselect')){
			$curOption.find('.a-option').hide();
			if(!abcSelectIsOk){
				abcSelect();
			}
		}else{
			toLink(false);
		}
		
	});
	//取消
	$btnCancel.on('click',function(){
		curIndex = $btnCancel.index($(this));
		$curOption = $Option.eq(curIndex);
		$curOption.removeClass('cur');
		if($curOption.data('abcselect')){
			$curOption.find('.a-option').show();
		}else{
			toLink(true);
		}
		
	});
	//链接转换
	function toLink(toLink){
		$links = $curOption.find('.a-option').find('a');
		if(toLink){
			$links.each(function(i,n){
				   $(n).attr('href',$(n).data('alink'));
			});
			$links.on('click',function(){
				$(this).children('input[type="checkbox"]').trigger('click');
			});
		}else{
			if(!$links.eq(0).data('alink')){
				$links.each(function(i,n){
					$(n).data('alink',$(n).attr('href'));
				});
			}
			$links.each(function(i,n){
				$(n).attr('href','javascript:void(0);');
			});
			$links.off('click');
		}
	}
	
	//首字母筛选
	function abcSelect(){
		abcSelectIsOk = true;
		var
			$aSelectNavItem = $Option.find('.a-select-nav').find('span'),
			$aOption2 = $Option.find('.a-option2'),
			$aOption2Item = $aOption2.find('.a-op2-item'),
			wrapH = $aOption2.outerHeight(true),
			countH = 0,
			itemHArry = [],
			curSIndex = 0,
			toTop = 0;
		$aOption2Item.each(function(i,n){
			var curH = $(n).outerHeight(true);
			itemHArry.push(curH);
			countH = countH + curH;
		});
		$aSelectNavItem.on('click',function(){
			toTop = 0;
			curSIndex = $aSelectNavItem.index($(this));
			for(var i = 0,len = curSIndex;i < len;i++){
				toTop = toTop + itemHArry[i];
			}
			$aOption2.animate({scrollTop:toTop},400);	
		});
	}
	
	//查看更多选项
	$attrsMore.on('click',function(){
		if(attrsMoreText === ''){
			attrsMoreText = $(this).text(); 
		}
		if($(this).text() === '收起'){
			$(this).text(attrsMoreText);
			$attrs.find('.a-itme').each(function(i,n){
				if($(n).hasClass('hide')){
					$(n).hide();
				}
			});
		}else{
			$(this).text('收起');
			$attrs.find('.a-itme').show();
		}
	});
}
/*
 * 浏览记录
 */
$(function(){
	 HY.menu();
	 HY.attrs();
});
